"""
UI 主题管理器，用于管理应用程序主题
"""
from typing import Dict, Any
from PyQt5.QtGui import QColor, QPalette, QFont
from PyQt5.QtWidgets import QApplication
from app.config.default_config import UI_THEME, UI_FONT_SIZE


class Theme:
    """
    UI 主题管理器，用于管理应用程序主题
    """
    # 主题颜色定义
    LIGHT_THEME = {
        "background": "#FFFFFF",
        "foreground": "#333333",
        "primary": "#2196F3",
        "secondary": "#03A9F4",
        "success": "#4CAF50",
        "warning": "#FFC107",
        "error": "#F44336",
        "info": "#2196F3",
        "border": "#E0E0E0",
        "hover": "#F5F5F5",
        "active": "#E3F2FD",
        "disabled": "#BDBDBD",
        "card_background": "#FFFFFF",
        "text_primary": "#212121",
        "text_secondary": "#757575",
        "text_disabled": "#9E9E9E",
        "icon": "#757575",
    }
    
    DARK_THEME = {
        "background": "#121212",
        "foreground": "#FFFFFF",
        "primary": "#2196F3",
        "secondary": "#03A9F4",
        "success": "#4CAF50",
        "warning": "#FFC107",
        "error": "#F44336",
        "info": "#2196F3",
        "border": "#333333",
        "hover": "#1E1E1E",
        "active": "#0D47A1",
        "disabled": "#757575",
        "card_background": "#1E1E1E",
        "text_primary": "#FFFFFF",
        "text_secondary": "#B0B0B0",
        "text_disabled": "#757575",
        "icon": "#B0B0B0",
    }
    
    _current_theme = UI_THEME
    
    @classmethod
    def get_current_theme(cls) -> str:
        """
        获取当前主题
        
        @return: 当前主题名称
        """
        return cls._current_theme
    
    @classmethod
    def set_theme(cls, theme_name: str) -> bool:
        """
        设置主题
        
        @param theme_name: 主题名称，可选 "light" 或 "dark"
        @return: 设置是否成功
        """
        if theme_name not in ["light", "dark"]:
            return False
        
        cls._current_theme = theme_name
        return True
    
    @classmethod
    def get_color(cls, color_name: str) -> str:
        """
        获取主题颜色
        
        @param color_name: 颜色名称
        @return: 颜色值
        """
        theme = cls.LIGHT_THEME if cls._current_theme == "light" else cls.DARK_THEME
        return theme.get(color_name, "#000000")
    
    @classmethod
    def get_qcolor(cls, color_name: str) -> QColor:
        """
        获取主题 QColor
        
        @param color_name: 颜色名称
        @return: QColor 对象
        """
        return QColor(cls.get_color(color_name))
    
    @classmethod
    def get_font(cls, size_offset: int = 0) -> QFont:
        """
        获取主题字体
        
        @param size_offset: 字体大小偏移量
        @return: QFont 对象
        """
        font = QFont()
        font.setPointSize(UI_FONT_SIZE + size_offset)
        return font
    
    @classmethod
    def apply_theme(cls, app: QApplication) -> None:
        """
        应用主题到应用程序
        
        @param app: QApplication 对象
        """
        palette = QPalette()
        
        # 设置应用程序调色板
        if cls._current_theme == "light":
            # 浅色主题
            palette.setColor(QPalette.Window, cls.get_qcolor("background"))
            palette.setColor(QPalette.WindowText, cls.get_qcolor("text_primary"))
            palette.setColor(QPalette.Base, cls.get_qcolor("card_background"))
            palette.setColor(QPalette.AlternateBase, cls.get_qcolor("hover"))
            palette.setColor(QPalette.ToolTipBase, cls.get_qcolor("background"))
            palette.setColor(QPalette.ToolTipText, cls.get_qcolor("text_primary"))
            palette.setColor(QPalette.Text, cls.get_qcolor("text_primary"))
            palette.setColor(QPalette.Button, cls.get_qcolor("background"))
            palette.setColor(QPalette.ButtonText, cls.get_qcolor("text_primary"))
            palette.setColor(QPalette.Link, cls.get_qcolor("primary"))
            # 修改选中项的背景色和文字颜色，使文字更清晰
            palette.setColor(QPalette.Highlight, QColor("#e3f2fd"))
            palette.setColor(QPalette.HighlightedText, QColor("#000000"))
        else:
            # 深色主题
            palette.setColor(QPalette.Window, cls.get_qcolor("background"))
            palette.setColor(QPalette.WindowText, cls.get_qcolor("text_primary"))
            palette.setColor(QPalette.Base, cls.get_qcolor("card_background"))
            palette.setColor(QPalette.AlternateBase, cls.get_qcolor("hover"))
            palette.setColor(QPalette.ToolTipBase, cls.get_qcolor("background"))
            palette.setColor(QPalette.ToolTipText, cls.get_qcolor("text_primary"))
            palette.setColor(QPalette.Text, cls.get_qcolor("text_primary"))
            palette.setColor(QPalette.Button, cls.get_qcolor("background"))
            palette.setColor(QPalette.ButtonText, cls.get_qcolor("text_primary"))
            palette.setColor(QPalette.Link, cls.get_qcolor("primary"))
            # 修改选中项的背景色和文字颜色，使文字更清晰
            palette.setColor(QPalette.Highlight, QColor("#0D47A1"))
            palette.setColor(QPalette.HighlightedText, QColor("#FFFFFF"))
        
        # 设置禁用状态的颜色
        palette.setColor(QPalette.Disabled, QPalette.WindowText, cls.get_qcolor("text_disabled"))
        palette.setColor(QPalette.Disabled, QPalette.Text, cls.get_qcolor("text_disabled"))
        palette.setColor(QPalette.Disabled, QPalette.ButtonText, cls.get_qcolor("text_disabled"))
        
        # 应用调色板
        app.setPalette(palette)
        
        # 设置应用程序字体
        app.setFont(cls.get_font())
        
        # 设置全局样式表
        app.setStyleSheet(cls.get_global_stylesheet())
    
    @classmethod
    def get_global_stylesheet(cls) -> str:
        """
        获取全局样式表
        
        @return: 全局样式表字符串
        """
        return """
            /* QGroupBox 样式 */
            QGroupBox {
                font-weight: bold;
                border: 1px solid #ddd;
                border-radius: 8px;
                margin-top: 8px;
                padding-top: 6px;
                background-color: #f8f9fa;
            }
            
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 6px;
            }
            
            /* 下拉框样式 */
            QComboBox {
                min-height: 24px;
                padding: 1px 6px;
                border-radius: 4px;
                border: 1px solid #ccc;
                font-size: 12px;
            }
            
            QComboBox:hover {
                border: 1px solid #aaa;
            }
            
            QComboBox:focus {
                border: 1px solid #4a90e2;
            }
            
            QComboBox::drop-down {
                subcontrol-origin: padding;
                subcontrol-position: top right;
                width: 20px;
                border-left-width: 1px;
                border-left-color: #ccc;
                border-left-style: solid;
            }
            
            /* 表格样式 */
            QTableWidget {
                border: 1px solid #ddd;
                border-radius: 3px;
                background-color: #fff;
                selection-background-color: rgba(0, 188, 212, 80);
                selection-color: #ffffff;
                gridline-color: #f0f0f0;
                font-size: 12px;
            }
            
            QTableWidget::item {
                min-height: 28px;
                padding: 2px 4px;
            }
            
            QTableWidget::item:selected {
                background-color: rgba(0, 188, 212, 80);
                color: #ffffff;
            }
            
            QHeaderView::section {
                background-color: #f5f5f5;
                padding: 4px;
                border: none;
                border-bottom: 1px solid #ddd;
                font-weight: bold;
                font-size: 12px;
            }
            
            /* 滑块样式 */
            QSlider::groove:horizontal {
                height: 6px;
                background: #f0f0f0;
                border-radius: 3px;
            }
            
            QSlider::handle:horizontal {
                background: #2196F3;
                border: none;
                width: 16px;
                margin: -5px 0;
                border-radius: 8px;
            }
            
            QSlider::sub-page:horizontal {
                background: #2196F3;
                border-radius: 3px;
            }
            
            /* 输入框样式 */
            QLineEdit {
                padding: 3px 6px;
                border-radius: 4px;
                border: 1px solid #ccc;
                font-size: 12px;
                min-height: 24px;
            }
            
            QLineEdit:focus {
                border: 1px solid #4a90e2;
            }
            
            /* 按钮样式 */
            QPushButton {
                border-radius: 4px;
                border: 1px solid #ddd;
                background-color: #f0f0f0;
                color: #333333;
                font-size: 12px;
            }
            
            QPushButton:hover {
                background-color: #e6e6e6;
                border-color: #ccc;
            }
            
            QPushButton:pressed {
                background-color: #d9d9d9;
            }
            
            QPushButton:disabled {
                background-color: #f8f8f8;
                color: #aaaaaa;
                border-color: #eaeaea;
            }
            
            /* 蓝底白字按钮样式 */
            QPushButton[primary="true"] {
                background-color: #2196F3;
                color: white;
                border: none;
                font-weight: bold;
                font-size: 11px;
            }
            
            QPushButton[primary="true"]:hover {
                background-color: #1976D2;
            }
            
            QPushButton[primary="true"]:pressed {
                background-color: #0D47A1;
            }
            
            QPushButton[primary="true"]:disabled {
                background-color: #BBDEFB;
                color: #E3F2FD;
            }
            
            /* 复选框样式 */
            QCheckBox {
                font-size: 12px;
            }
        """ 